<template>
  <el-container class="main-layout">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '240px'" class="sidebar">
      <!-- 系统标题 -->
      <div class="sidebar-header">
        <div class="logo-section">
          <span v-show="!isCollapse" class="system-name">SG3L Admin</span>
        </div>
      </div>
      
      <!-- 主导航 -->
      <div class="main-navigation">
        <div v-show="!isCollapse" class="nav-title">主导航</div>
        
        <el-menu
          :default-active="$route.path"
          :collapse="isCollapse"
          :router="true"
          background-color="transparent"
          text-color="#64748b"
          active-text-color="#6366f1"
          class="sidebar-menu"
        >
          <!-- 仪表盘 - 所有用户都可访问 -->
          <el-menu-item index="/dashboard" class="menu-item">
            <el-icon><Odometer /></el-icon>
            <template #title>仪表盘</template>
          </el-menu-item>
          
          <!-- 管理员专用菜单 -->
          <template v-if="userStore.isAdmin">
            <el-menu-item index="/documents" class="menu-item">
              <el-icon><Document /></el-icon>
              <template #title>文档管理</template>
            </el-menu-item>
            
            <el-menu-item index="/conversations" class="menu-item">
              <el-icon><ChatDotRound /></el-icon>
              <template #title>对话历史</template>
            </el-menu-item>
            
            <el-menu-item index="/users" class="menu-item">
              <el-icon><Setting /></el-icon>
              <template #title>用户管理</template>
            </el-menu-item>
          </template>
        </el-menu>
      </div>
    </el-aside>

    <el-container class="content-container">
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-content">
          <!-- 左侧空白区域 -->
          <div class="header-left-spacer"></div>
          
          <!-- 右侧用户信息 -->
          <div class="header-actions">
            <!-- 用户信息 -->
            <el-dropdown @command="handleCommand" class="user-dropdown">
              <div class="user-profile">
                <el-avatar :size="36" :src="userAvatarUrl" class="user-avatar" />
                <div class="user-info-text">
                  <span class="username">{{ userInfo.username || '用户' }}</span>
                  <span class="user-role">{{ userStore.isAdmin ? '系统管理员' : '普通用户' }}</span>
                </div>
                <el-icon class="dropdown-arrow"><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    个人资料
                  </el-dropdown-item>
                  <el-dropdown-item command="logout" divided>
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <!-- 主内容区 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import {
  Fold,
  Expand,
  ArrowDown,
  Odometer,
  Document,
  Upload,
  User,
  ChatDotRound,
  Setting,
  QuestionFilled,
  SwitchButton
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const isCollapse = ref(false)

const defaultAvatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'

// 头像URL处理函数 - 解决Gitee图床CORS问题
const processAvatarUrl = (url) => {
  if (!url) return url
  // 只要是http开头的都走代理，兼容gitee、oss等
  if (url.startsWith('http')) {
    return `https://images.weserv.nl/?url=${encodeURIComponent(url.replace(/^https?:\/\//, ''))}`
  }
  return url
}

const currentPageTitle = computed(() => {
  return route.meta?.title || '页面'
})

const userInfo = computed(() => {
  return userStore.userInfo || { username: '管理员', avatar: '' }
})

// 计算头像URL
const userAvatarUrl = computed(() => {
  const avatar = userInfo.value.avatar
  if (avatar && avatar.trim()) {
    return processAvatarUrl(avatar)
  }
  return defaultAvatar
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'logout':
      try {
        await userStore.logout()
        ElMessage.success('退出成功')
        router.push('/login')
      } catch (error) {
        ElMessage.error('退出失败')
      }
      break
  }
}

onMounted(() => {
  // 检查用户登录状态
  if (!userStore.isLoggedIn) {
    router.push('/login')
  }
})
</script>

<style scoped>
.main-layout {
  min-height: 100vh;
  background-color: #f8fafc;
  display: flex;
}

.content-container {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 侧边栏样式 */
.sidebar {
  background-color: #ffffff;
  border-right: 1px solid #e2e8f0;
  transition: width 0.3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
}

.sidebar-header {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid #e2e8f0;
  background-color: #ffffff;
}

.logo-section {
  display: flex;
  align-items: center;
  width: 100%;
}

.system-name {
  font-size: 16px;
  font-weight: 600;
  color: #1e293b;
  white-space: nowrap;
}

/* 导航区域 */
.main-navigation {
  padding: 20px 16px 16px;
}

.nav-title {
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
  padding-left: 12px;
}

.sidebar-menu {
  border: none;
}

.sidebar-menu .menu-item {
  margin-bottom: 4px;
  border-radius: 8px;
  transition: all 0.2s ease;
}

.sidebar-menu .menu-item:hover {
  background-color: #f1f5f9 !important;
}

.sidebar-menu .menu-item.is-active {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
  color: #ffffff !important;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.sidebar-menu .menu-item.is-active .el-icon {
  color: #ffffff !important;
}

/* 内容容器 */
.content-container {
  background-color: #f8fafc;
}

/* 头部样式 */
.header {
  background-color: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  height: 64px;
  padding: 0;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
  max-width: 100%;
}

.header-left-spacer {
  flex: 1;
}

.header-actions {
  display: flex;
  align-items: center;
}

.user-dropdown {
  cursor: pointer;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  border-radius: 20px;
  transition: all 0.2s ease;
  border: 1px solid transparent;
}

.user-profile:hover {
  background-color: #f9fafb;
  border-color: #e5e7eb;
}

.user-avatar {
  border: 2px solid #e5e7eb;
}

.user-info-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.username {
  font-size: 14px;
  font-weight: 600;
  color: #111827;
  line-height: 1;
}

.user-role {
  font-size: 12px;
  color: #6b7280;
  line-height: 1;
}

.dropdown-arrow {
  font-size: 12px;
  color: #9ca3af;
  transition: transform 0.2s ease;
}

.user-dropdown:hover .dropdown-arrow {
  transform: rotate(180deg);
}

/* 主内容区 */
.main-content {
  padding: 24px;
  background-color: #f8fafc;
  flex: 1;
  overflow: visible; /* 确保使用页面主滚动条 */
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }
  
  .user-info-text {
    display: none;
  }
  
  .main-content {
    padding: 16px;
  }
}

/* Element Plus 组件样式覆盖 */
:deep(.el-menu-item) {
  border-radius: 8px !important;
  margin-bottom: 4px !important;
}

:deep(.el-menu-item:hover) {
  background-color: #f1f5f9 !important;
}

:deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
  color: #ffffff !important;
}

:deep(.el-dropdown-menu__item) {
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.el-dropdown-menu__item:hover) {
  background-color: #f9fafb;
  color: #6366f1;
}
</style>
